package com.woniuxy.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Map;

/**
 * @author dingpf
 * @version 1.0
 * @description
 * @create 2025/4/25 11:26
 */
public class UserConenxt {

    public static Integer getUserId() throws ParseException {
        //RequestContextHolder ：tomcat封装请求上下文类
        ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();

        String jwt = request.getHeader("authorization");
        Map userInfo = JWTUtil.getJWTUserInfo(jwt);
        return ((Long)userInfo.get("id")).intValue();

    }
}
